<?php
class Muser extends CI_Model{
	function __construct(){
		parent::__construct();
	}
	//验证手机号是否已注册并验证通过过
	function checkPhoneForRegister($phone){
		$sql = "select *
				from tbuserinfo 
				where phone = ? and status = 1";
		$query = $this->db->query($sql,array("$phone"));
		$exists = false;
		foreach ($query->result() as $row){
			return true;
		}
		return $exists;
	}
    //验证手机号是否已注册并验证通过过
    function checkPhone($uid){
        $sql = "select *
				from tbuserinfo
				where id = ? and status = 1";
        $query = $this->db->query($sql,array($uid));
        $exists = false;
        foreach ($query->result() as $row){
            return true;
        }
        return $exists;
    }
	function checkPhoneExists($phone){
		$sql = "select *
				from tbuserinfo
				where phone = ?";
		$query = $this->db->query($sql,array("$phone"));
		$exists = false;
		foreach ($query->result() as $row){
			return true;
		}
		return $exists;
	}
	//写入手机号
	function registerPhone($phone){
		$sql = "insert into tbuserinfo(phone,status) values(?,0)";
		$query = $this->db->query($sql,array("$phone"));
		return $this->db->affected_rows();
	}
	//验证手机号验证符是否正确，
	function checkSmsCode($phone,$smscode){
		$sql = "select *
				from tbuserinfo
				where phone = ? and checkcode = ? and status= 0";
		$query = $this->db->query($sql,array("$phone",$smscode));
		foreach ($query->result() as $row){
			return true;
		}
		return false;
	}
	function updateLogin($phone,$pwd,$realname,$smscode,$sex){
		$sql = "update tbuserinfo 
				set pwd = ?,realname = ?,checkcode=?,sex=?,status=1,regtime=now()
				where phone = ?";
		$query = $this->db->query($sql,array("$pwd","$realname","$smscode","$sex","$phone"));
		return $this->db->affected_rows();
	}
	function saveEmailLogin ( $email, $pwd, $realname, $sex,$guid ){
		
		
		$sql = "insert into tbuserinfo(email,pwd,realname,sex,checkguid) values(?,?,?,?,?)";
		$this->db->query($sql,array("$email","$pwd","$realname","$sex","$guid"));
		return $this->db->affected_rows();
	}
	function checkEmailForRegister($email){
		$sql = "select *
				from tbuserinfo
				where email = ? and status = 1";
		$query = $this->db->query($sql,array("$email"));
		foreach ($query->result() as $row){
			return true;
		}
		return false;
	}
	function checkLogin($phone,$pwd){
		$sql = "select *
				from tbuserinfo
				where pwd=? and (phone=? or email=?) and isvalid = 1 and status=1";
		$query = $this->db->query($sql,array("$pwd","$phone","$phone"));
		return $query->result();
	}
	function updateCheckCode($phone,$code){
		$sql = " update tbuserinfo 
				set checkcode=?
				where phone=?";
		$query = $this->db->query($sql,array("$code","$phone"));
		return $this->db->affected_rows();
	}	
	function updateHeadUrl($id,$headurl){
		$sql =" update tbuserinfo
				set headurl = ?
				where id= ?";
		$query = $this->db->query($sql,array("$headurl","$id"));
		return $this->db->affected_rows();
	}
	function searchModel($cityid,$modeltype,$minHeight,$maxHeight,$minAge,$maxAge,$sex,$age){
		$sql = "select ui.*,p.name provincename,c.name cityname,p1.name jgprovincename,c1.name jgcityname,
				(YEAR(CURDATE())-YEAR(birthday)) - (RIGHT(CURDATE(),5)<RIGHT(birthday,5))   AS age
				from tbuserinfo ui
				left join tbprovince p on ui.provinceid= p.id
				left join tbcity c on ui.cityid = c.id
				left join tbprovince p1 on ui.jgprovinceid = p1.id
				left join tbcity c1 on ui.jgcityid = c1.id
				where ui.status = 1 and ui.isvalid=1 
					and (ui.high is not null and ui.weight is not null and ui.provinceid is not null and ui.headurl is not null) ";
		
		if($cityid!="0"){
			$sql.=" and ui.cityid = $cityid";
		}
		if($modeltype!="0"){
			$sql.=" and ui.modeltype like '%/$modeltype/%'";
		}
		if($minHeight!="0"){
			$sql.=" and ui.high>=$minHeight";
		}
		if($maxHeight!="0"){
			$sql.=" and ui.high<=$maxHeight";
		}
		if($age=="" && $minAge!="0"){
			$sql.=" and ((YEAR(CURDATE())-YEAR(birthday)) - (RIGHT(CURDATE(),5)<RIGHT(birthday,5)))>=$minAge";
		}
		if($age =="" && $maxAge!="0"){
			$sql.=" and ((YEAR(CURDATE())-YEAR(birthday)) - (RIGHT(CURDATE(),5)<RIGHT(birthday,5)))<=$maxAge";
		}
		if($sex!=""){
			$sql.=" and sex='$sex'";
		}
		if($age!=""){
			$sql.=" and ((YEAR(CURDATE())-YEAR(birthday)) - (RIGHT(CURDATE(),5)<RIGHT(birthday,5)))=$age";
		}
		
		$query = $this->db->query($sql);
		return $query->result();
	}
	function modifyPV($id){
		$sql = "update tbuserinfo set pvcount=pvcount+1 
				where id=?";
		$this->db->query($sql,array("$id"));
	}
	function getById($id){
		$arr = array();
		$sql = "select ui.*,p.name provincename,c.name cityname,p1.name jgprovincename,c1.name jgcityname,
				(YEAR(CURDATE())-YEAR(birthday)) - (RIGHT(CURDATE(),5)<RIGHT(birthday,5))   AS age
				from tbuserinfo ui
				left join tbprovince p on ui.provinceid= p.id
				left join tbcity c on ui.cityid = c.id
				left join tbprovince p1 on ui.jgprovinceid = p1.id
				left join tbcity c1 on ui.jgcityid = c1.id
				where ui.id = ?";
		$query = $this->db->query($sql,array("$id"));
		foreach ($query->result() as $row){
			$arr["id"]=empty($row->id)?"":$row->id;
			$arr["realname"]=empty($row->realname)?"":$row->realname;
			$arr["birthday"]=empty($row->birthday)?"":$row->birthday;

			$arr["email"]=empty($row->email)?"":$row->email;
			$arr["phone"]=empty($row->phone)?"":$row->phone;
			$arr["qq"]=empty($row->qq)?"":$row->qq;
			$arr["pwd"]=empty($row->pwd)?"":$row->pwd;
			$arr["checkcode"]=empty($row->checkcode)?"":$row->checkcode;
			$arr["sex"]=empty($row->sex)?"":$row->sex;
			$arr["status"]=empty($row->status)?"":$row->status;
			$arr["headurl"]=empty($row->headurl)?"":$row->headurl;
			$arr["cardid"]=empty($row->cardid)?"":$row->cardid;
			$arr["provinceid"]=empty($row->provinceid)?"":$row->provinceid;
			$arr["provincename"] = empty($row->provincename)?"":$row->provincename;
			$arr["cityid"]=empty($row->cityid)?"":$row->cityid;
			$arr["cityname"] = empty($row->cityname)?"":$row->cityname;
			$arr["jgprovinceid"]=empty($row->jgprovinceid)?"":$row->jgprovinceid;
			$arr["jgprovincename"] = empty($row->jgprovincename)?"":$row->jgprovincename;
			$arr["jgcityid"]=empty($row->jgcityid)?"":$row->jgcityid;
			$arr["jgcityname"] = empty($row->jgcityname)?"":$row->jgcityname;
			$arr["high"]=empty($row->high)?"":$row->high;
			$arr["weight"]=empty($row->weight)?"":$row->weight;
			$arr["shoesize"]=empty($row->shoesize)?"":$row->shoesize;
			$arr["chestsize"]=empty($row->chestsize)?"":$row->chestsize;
			$arr["waistsize"]=empty($row->waistsize)?"":$row->waistsize;
			$arr["buttsize"]=empty($row->buttsize)?"":$row->buttsize;
			$arr["worknature"]=empty($row->worknature)?"":$row->worknature;
			$arr["modeltype"]=empty($row->modeltype)?"":$row->modeltype;
			$arr["modelstyle"]=empty($row->modelstyle)?"":$row->modelstyle;
			$arr["modelhair"]=empty($row->modelhair)?"":$row->modelhair;
			$arr["modelskin"]=empty($row->modelskin)?"":$row->modelskin;
			$arr["modelbody"]=empty($row->modelbody)?"":$row->modelbody;
            $arr["pvcount"]= !isset($row->pvcount)?"":$row->pvcount;
			$arr["activitiexperience"]=empty($row->activitiexperience)?"":$row->activitiexperience;
			$arr["trainingexperience"]=empty($row->trainingexperience)?"":$row->trainingexperience;
			$arr["competitionexperience"]=empty($row->competitionexperience)?"":$row->competitionexperience;
			$arr["reward"]=empty($row->reward)?"":$row->reward;
			$arr["cardcontent"] = empty($row->cardcontent)?"":$row->cardcontent;
			$arr["age"] = !isset($row->age)?"":$row->age;
			$arr["idcardurl"] = !isset($row->idcardurl)?"":$row->idcardurl;
			$arr["idcardstatus"] = !isset($row->idcardstatus)?"":$row->idcardstatus;
			$arr["idcardno"] = !isset($row->idcardno)?"":$row->idcardno;
            $arr["codeimg"] = !isset($row->codeimg)?"":$row->codeimg;
            $arr["supportCount"] = !isset($row->supportCount)?"":$row->supportCount;
            $arr["voteCount"] = !isset($row->voteCount)?"":$row->voteCount;
            $dateYear = date('Y',time());
            if(!empty($row->birthday)){
                $arr["age"] =  (int)$dateYear-substr($row->birthday,0,4);
            }
			//增加模特类型
			$arr["modeltypename"]="";
			$result = "";
			$modeltype = $arr["modeltype"];
			$arrType = explode("/", $modeltype);
			$type= 2;
			foreach ($arrType as $classid){
				if($classid!=""){					
					$sqlType = "select *
							from tbdic where classid = ? and type = 2";
					$queryType = $this->db->query($sqlType,array("$classid"));
					foreach ($queryType->result() as $rowType){
						$arr["modeltypename"].="  ".$rowType->name;
					} 
				}
			}
			return $arr;
		}
		return $arr;
	}
	
	function getModelTypeName($modeltype){
		$arrType = explode("/", $modeltype);
		$result="";
		$type= 2;
		foreach ($arrType as $classid){
			if($classid!=""){
				$sqlType = "select *
							from tbdic where classid = ? and type = 2";
				$queryType = $this->db->query($sqlType,array("$classid"));
				foreach ($queryType->result() as $rowType){
					$result.="  ".$rowType->name;
				}
			}
		}
		return $result;
	}
	
	function saveArchiceOne ( $id, $realname, $birthday, $provinceid, $cityid, 
							$jgprovinceid, $jgcityid, $phone, $qq,$sex ){
		$sql = "update tbuserinfo
				set realname= ?,birthday = ?,provinceid = ?,cityid = ?,jgprovinceid = ?,jgcityid = ?,
					phone = ?,qq = ?, sex = ?
				where id = ?";
		$query = $this->db->query($sql,array("$realname","$birthday","$provinceid","$cityid",
											"$jgprovinceid","$jgcityid","$phone","$qq","$sex","$id"));
		return $this->db->affected_rows();
	}
	
	function saveArchiveTwo($id,$high,$weight,$shoesize,$chestsize,$waistsize,$buttsize,
							$worknature,$modeltype,$modelstyle,$modelhair,$modelskin,$modelbody){
		$sql = "update tbuserinfo
				set high = ?,weight = ?,shoesize = ?,chestsize = ?,waistsize = ?, buttsize = ?,
					worknature = ?,modeltype = ?,modelstyle = ?,modelhair = ?,modelskin = ?,modelbody = ?
				where id = ?";
		$query = $this->db->query($sql,array("$high","$weight","$shoesize","$chestsize","$waistsize","$buttsize",
							"$worknature","$modeltype","$modelstyle","$modelhair","$modelskin","$modelbody","$id"));
		return $this->db->affected_rows();
	}
	function saveExperience($id,$activity,$training,$competition,$reward){
		$sql = "update tbuserinfo
				set activitiexperience = ?,trainingexperience = ?,competitionexperience = ?,reward = ?
				where id = ?";
		$query = $this->db->query($sql,array("$activity","$training","$competition","$reward","$id"));
		return $this->db->affected_rows();
	}
	function saveCard($id,$cardid,$cardcontent){
		$sql = "update tbuserinfo
				set cardid = ?,cardcontent = ?
				where id = ?";
		$query = $this->db->query($sql,array("$cardid","$cardcontent","$id"));
		return $this->db->affected_rows();
	}
	function checkPwd($id,$pwd){
		$sql = "select *
				from tbuserinfo
				where id = ? and pwd = ?";
		$query = $this->db->query($sql,array("$id","$pwd"));
        echo $this->db->last_query();
		foreach ($query->result() as $row){
			return true;
		}
		return false;
	}
	function modifyPwd($id,$pwd,$pwdnew){
		$sql = "update tbuserinfo
				set pwd = ?
				where id= ? and pwd = ?";
		$this->db->query($sql,array("$pwdnew","$id","$pwd"));
		return $this->db->affected_rows();
	}
	function updateIdCard($id,$url){
		$sql = "update tbuserinfo
				set idcardurl = ?,idcardstatus = 0
				where id = ?";
		$this->db->query($sql,array("$url","$id"));
		return $this->db->affected_rows();
	}
	function updateIdCard2($id,$url){
		$sql = "update tbuserinfo
				set idcardurl2 = ?,idcardstatus = 0
				where id = ?";
		$this->db->query($sql,array("$url","$id"));
		return $this->db->affected_rows();
	}
	function updateIdCardNo($id,$idcardno){
		$sql = "update tbuserinfo
				set idcardno = ?,idcardstatus = 0
				where id = ?";
		$this->db->query($sql,array("$idcardno","$id"));
		return $this->db->affected_rows();
	}
	function saveModifyPhone ($id, $phone, $code ){
		$sql = "update tbuserinfo
				set modifyphone = ?, checkcode = ?
				where id = ?";
		$this->db->query($sql,array("$phone","$code","$id"));
		return $this->db->affected_rows();
	}
	function checkModifyCode($phone ,$code){
		$sql = "select *
				from tbuserinfo
				where modifyphone = ? and checkcode = ?";
		$query = $this->db->query($sql,array("$phone","$code"));
		foreach ($query->result() as $row){
			return true;
		}
		return false;
	}
	function updateModifyPhone($id){
		$sql = "update tbuserinfo
				set phone = modifyphone
				where id = ?";
		$this->db->query($sql,array("$id"));
		return $this->db->affected_rows();
	}
	function registerActivate($email,$checkcode){
		$sql = "update tbuserinfo set status = 1 
				where email = ? and checkguid = ?";
		$this->db->query($sql,array("$email","$checkcode"));


        $sql2 = "select *
            from tbuserinfo
            where email = ? and checkguid = ? and status=1 limit 1";
        $query = $this->db->query($sql2,array("$email","$checkcode"));

        return $query->row_array();
	}
	
	function getCheckEmailUrl($domain){
		$sql = "select * from tbemail_url_info where domain = ?";
		$query = $this->db->query($sql,array("$domain"));
		foreach ($query->result() as $row){
			return $row->url;
		}
		return "";
	}

    /**
     * 检测用户资料是否完善
     * @param $uid 用户uid
     */
    function checkUserDataIsPerFect($uid)
    {
        $query = $this->db->get_where('tbuserinfo', array('id' => $uid), 1);

        $check = true;
        if ($query->num_rows() > 0)
        {
            $user = $query->row_array();
            $fieldArr = array('realname','birthday','phone','qq','sex','headurl','provinceid','cityid','high','shoesize','chestsize','waistsize','buttsize','worknature','modeltype','modelstyle','modelhair','modelskin','modelbody','activitiexperience','trainingexperience','competitionexperience','reward','jgprovinceid','jgcityid');
            for($i=0;$i<count($fieldArr);$i++){
                if(empty($user[$fieldArr[$i]])){
                    $check = false;
                    break;
                }
            }
        }
        return $check;
    }

    function checkUserMadouKa($uid)
    {
        $query = $this->db->get_where('tbuserinfo', array('id' => $uid), 1);
        $user = $query->row_array();
        $chek = false;
        if(!empty($user['cardcontent'])){
            $chek = true;
        }
        return $chek;
    }

    /**
     * 更新用户真实姓名
     * @param $uid 用户uid
     * @param $realname 用户真实姓名
     * @return mixed
     */
    function updateUserRealName($uid,$realname)
    {
        $sql = "update tbuserinfo
				set `realname` = ?
				where `id` = ?";
        $this->db->query($sql,array("$realname",$uid));
        return $this->db->affected_rows();
    }

    /**
     * 根据uid获取用户基本信息和
     * @param $uid
     */
    function getUserByUid($uid)
    {
        $sql = "select * from `tbuserinfo` where `id` = ?";
        $query = $this->db->query($sql,array($uid));

        return $query->row_array();
    }

    /**
     * 设置用户二维码
     * @param $uid
     * @param $userCodeImg
     */
    function setUserCode($uid,$userCodeImg)
    {
        $sql = "update tbuserinfo
				set `codeimg` = ?
				where `id` = ?";
        $this->db->query($sql,array("$userCodeImg",$uid));
        return $this->db->affected_rows();
    }

    function support($id){
        $sql = "update tbuserinfo
				set supportCount = supportCount+1
				where id = ?";
        $this->db->query($sql,array("$id"));
        return $this->db->affected_rows();
    }

    /**
     * 投票微访谈
     * @param $id 用户uid
     * @return mixed
     */
    function voteInterview($id){
        $sql = "update tbuserinfo
				set `voteCount` = voteCount+1
				where id = ?";
        $this->db->query($sql,array("$id"));
        return $this->db->affected_rows();
    }

    /**
     * 获取用户头像
     * @return mixed
     */
    function getUserPhoto($uid)
    {
        $sql = "select * from `tbuserinfo` where `id` = ?";
        $query = $this->db->query($sql,array($uid));

        $data = $query->row_array();
        $info['photoUrl'] = isset($data['headurl']) && !empty($data['headurl'])?$data['headurl']:'';

        return $info;
    }
}
?>